#include "CTimer.h"



CTimer::CTimer()
{
	// seed random timer for anything using rand
	srand((unsigned int)time(NULL));

	m_nFPS = 0;
	m_fTimeStart = 0.0f;
	m_fTimeDelta = 0.0f;
}


CTimer::~CTimer()
{

}



void CTimer::Reset()
{
	LARGE_INTEGER   nTicks;

	// get processor frequency
	QueryPerformanceFrequency(&m_tTickFrequency);

	// get current tick count
	QueryPerformanceCounter(&nTicks);

	// init time at start
	m_fTimeStart = (float)nTicks.QuadPart / (float)m_tTickFrequency.QuadPart;

	// reset frame count
	m_nFPS = 0;
}



void CTimer::Update()
{
	// locals
	static float   fTimePrev = GetTime();
	static float   fTimeLastFrame = GetTime();
	static int     nFrames = 0;
	float          fTimeNow = GetTime();

	// increment frame count
	nFrames++;

	// log how many frames since ONE second
	if (fTimeNow - fTimePrev > 1.0f)
	{
		m_nFPS = nFrames / (int)(fTimeNow - fTimePrev);
		nFrames = 0;
		fTimePrev = fTimeNow;
	}

	// calculate delta time
	m_fTimeDelta = (fTimeNow - fTimeLastFrame);
	fTimeLastFrame = fTimeNow;
}



float CTimer::GetTime()
{
	LARGE_INTEGER   tTicks;
	float           fTime;

	// get current tick count
	QueryPerformanceCounter(&tTicks);

	// get current time ( since computer turned on )
	fTime = (float)tTicks.QuadPart / (float)m_tTickFrequency.QuadPart;

	// calculate time since reset
	return (fTime - m_fTimeStart);
}